local skel = fk.CreateSkill {
  name = "lb__lixue",
}

Fk:loadTranslationTable {
  ["lb__lixue"] = "立雪",
  [":lb__lixue"] = "每回合限一次，你的阶段开始前，你可以交给一名其他角色一张牌，其代你执行此阶段；此阶段其获得牌/使用牌后，你可以摸/使用一张牌。若你均执行，你摸一张牌，额外执行此阶段并重置〖立雪〗。",

  ["#lb__lixue"] = "立雪：你可以交给一名其他角色一张牌，令其代你执行%arg",
  ["#lb__lixue-draw"] = "立雪：你可以摸一张牌",
  ["#lb__lixue-use"] = "立雪：你可以使用一张牌",

  ["@@lb__lixue-phase"] = "立雪",

  ["$lb__lixue1"] = "劳您担待了！",
  ["$lb__lixue2"] = "师傅，请喝茶！",
  ["$lb__lixue3"] = "哦！我全都懂了！",
  ["$lb__lixue4"] = "是时候展现真正的剑术了！",
}

skel:addEffect(fk.EventPhaseChanging, {
  mute = true,
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and not player:isNude()
        and player:usedSkillTimes(skel.name) == 0 and data.reason == "game_rule"
        and table.contains({ Player.Start, Player.Judge, Player.Draw, Player.Play, Player.Discard, Player.Finish },
          data.phase)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ps, c, s = room:askToChooseCardsAndPlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      min_card_num = 1,
      max_card_num = 1,
      equal = true,
      prompt = "#lb__lixue:::" .. Util.PhaseStrMapper(data.phase),
      skill_name = "lb__lixue",
    })
    if s then
      event:setCostData(self, { tos = ps, cards = c })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local to = cost_data.tos[1]
    room:obtainCard(to, cost_data.cards, false, fk.ReasonGive, player, skel.name)
    room:setPlayerMark(to, "@@lb__lixue-phase", 1)
    player:broadcastSkillInvoke(skel.name, math.random(1, 2))
    player.room:notifySkillInvoked(player, skel.name)
    to:gainAnExtraPhase(data.phase, skel.name, false)
    local n = #player:getTableMark("lb__lixue-turn")
    room:setPlayerMark(player, "lb__lixue-turn", 0)
    if n < 2 then
      data.skipped = true
    else
      player:broadcastSkillInvoke(skel.name, math.random(3, 4))
      player.room:notifySkillInvoked(player, skel.name)
      player:drawCards(1, skel.name)
      player:setSkillUseHistory(skel.name)
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  mute = true,
  is_delay_effect = true,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill("lb__lixue") then
      for _, move in ipairs(data) do
        if move.to and move.to:getMark("@@lb__lixue-phase") > 0 and move.toArea == Player.Hand then
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__lixue-draw"})
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, "lb__lixue")
    player.room:addTableMarkIfNeed(player, "lb__lixue-turn", 1)
  end,
})

skel:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill("lb__lixue") and target:getMark("@@lb__lixue-phase") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local use = player.room:askToPlayCard(player, {
      skill_name = "lb__lixue",
      skip = true,
      prompt = "#lb__lixue-use",
    })
    event:setCostData(self, { u = use })
    return use
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).u)
    player.room:addTableMarkIfNeed(player, "lb__lixue-turn", 2)
  end,
})

return skel
